package com.tywj.buscustomerapp.presenter;

import android.os.Build;
import android.util.Log;

import com.amap.api.maps.model.LatLng;
import com.tywj.buscustomerapp.model.SearchModel;
import com.tywj.buscustomerapp.model.bean.SearchUtilsBean;
import com.tywj.buscustomerapp.model.bean.StationBean;
import com.tywj.buscustomerapp.model.bean.TravelLineBean;
import com.tywj.buscustomerapp.presenter.contract.SearchContract;
import com.tywj.buscustomerapp.view.activity.LoginActivity;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class SearchPresenter implements SearchContract.SearchPersenter {

    private SearchContract.SearchView view;
    private SearchContract.SearchModel model;


    public SearchPresenter(SearchContract.SearchView view) {
        this.view = view;
        this.model = new SearchModel();
    }

    @Override
    public void startGetStation(final String cityId, final LatLng start, final LatLng end) {
        Observable.create(new ObservableOnSubscribe<List<SearchUtilsBean>>() {
            @Override
            public void subscribe(ObservableEmitter<List<SearchUtilsBean>> e){
                HashMap<SearchUtilsBean,SearchUtilsBean> map = model.getStations(cityId,start,end);
                HashMap<SearchUtilsBean,SearchUtilsBean> map1 = new HashMap<>();
                List<SearchUtilsBean> list = new ArrayList<>();
                if (map.size()>0) {
                    for (SearchUtilsBean bean:map.keySet()) {
                        if (bean.getStationId()<=map.get(bean).getStationId()){
                            map1.put(bean,map.get(bean));
                        }else{
                            //这里是否判断加入返程路线
//                            map1.put(map.get(bean),bean);
                        }
                    }
                    if (map1.size()>0){
                        String request = "";
                        for (SearchUtilsBean bean:map1.keySet()) {
                            request = request+bean.getLinNumber()+",";
                        }
                        List<TravelLineBean> travelLineBeans = model.getLineById(request);
//                        Log.i("SearchPresenter1",travelLineBeans.toString());
                        if (travelLineBeans!=null && travelLineBeans.size()>0){
                            for (SearchUtilsBean bean:map1.keySet()) {
                                if (start.latitude==end.latitude && start.longitude == end.longitude){
                                    bean.setEndBean(map1.get(bean));
                                    for (int i = 0; i < travelLineBeans.size(); i++) {
                                        if (bean.getLinNumber().equals(travelLineBeans.get(i).getNumber())){
                                            bean.setTravelLineBean(travelLineBeans.get(i));
                                        }
                                    }
                                    list.add(bean);
                                }else{
                                    if (!bean.getStationName().equals(map1.get(bean).getStationName())){
                                        bean.setEndBean(map1.get(bean));
                                        for (int i = 0; i < travelLineBeans.size(); i++) {
                                            if (bean.getLinNumber().equals(travelLineBeans.get(i).getNumber())){
                                                bean.setTravelLineBean(travelLineBeans.get(i));
                                            }
                                        }
                                        list.add(bean);
                                    }
                                }
                            }
                            if (list.size()>0){
//                                Log.i("taylor",list.size()+"");
                                Collections.sort(list, new Comparator<SearchUtilsBean>(){
                                    public int compare(SearchUtilsBean p1, SearchUtilsBean p2) {
                                        if(p1.getDistance() > p2.getDistance()){
                                            return 1;
                                        }
                                        if(p1.getDistance() == p2.getDistance()){
                                            return 0;
                                        }
                                        return -1;
                                    }
                                });
                                e.onNext(list);
                            }else {
                                e.onError(null);
                            }
                    }else{
                        e.onError(null);
                    }
                    }else{
                        e.onError(null);
                    }
                }else{
                    e.onError(null);
                }
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<SearchUtilsBean>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        if (view!=null){
                            view.startLoading();
                        }
                    }

                    @Override
                    public void onNext(List<SearchUtilsBean> s) {
                        if (view!=null){
                            view.showSearchResoult(s);
                            view.closeLoading();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
//                        Log.i("taylor",e.toString());
                        if (view!=null){
                            view.closeLoading();
                            view.showError("暂无线路");
                        }

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void onDestroy() {
        view = null;
        System.gc();
    }
}
